package com.example.prom.qianduoduo.y2025.controller;

import com.alibaba.fastjson.JSON;
import com.example.prom.qianduoduo.y2025.model.SacinfoModel;
import com.example.prom.strategy.excel.CustomCellStyleStrategy;
import com.example.prom.strategy.excel.CustomColumnWidthStrategy;
import com.example.prom.utils.DateUtil;
import com.example.prom.utils.HttpClientSingleton;
import com.example.prom.utils.HttpNewUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.alibaba.excel.EasyExcelFactory.write;

/**
 * Description
 *
 * @author DangWen
 */
public class SacinfoTestBack {

    public static void main(String[] args) {

        String baseUrl = "https://dbba.sacinfo.org.cn/";

        HttpClient httpClient = HttpClientSingleton.getLongCustomHttpClient();
        long startTime = System.currentTimeMillis();

        try {

            Map<String, String> headers = new HashMap<>();
            headers.put("Accept-Language","zh-CN,zh;q=0.9");
            headers.put("Cookie","Hm_lvt_36f2f0446e1c2cda8410befc24743a9b=1751339940,1751416952,1751419643; HMACCOUNT=8134B8F59C308B5F; Hm_lpvt_36f2f0446e1c2cda8410befc24743a9b=1751420045");
            headers.put("Accept-Encoding","gzip, deflate, br, zstd");
            headers.put("Referer","https://dbba.sacinfo.org.cn/stdList");
            headers.put("Accept","application/json, text/javascript, */*; q=0.01");
            headers.put("Content-Type","application/x-www-form-urlencoded");
            headers.put("X-Requested-With","XMLHttpRequest");
            headers.put("sec-ch-ua-mobile","?0");
            headers.put("sec-ch-ua-platform","\"Windows\"");
            headers.put("sec-ch-ua","\"Google Chrome\";v=\"137\", \"Chromium\";v=\"137\", \"Not/A)Brand\";v=\"24\"");
            headers.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36");
            headers.put("sec-fetch-site","same-origin");
            headers.put("sec-fetch-mode","cors");
            headers.put("sec-fetch-dest","empty");
            headers.put("Host","dbba.sacinfo.org.cn");
            headers.put("Connection","keep-alive");
            headers.put("Origin","https://dbba.sacinfo.org.cn");
            WebDriver webDriver = getWebDriver();
            WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(30));
            List<SacinfoModel> resultList = new ArrayList<>();
            int pages = 785;
//            int pages = 2;
            for (int i = 1; i <= pages; i++) {
                int sleepTime = 500 + new Random().nextInt(500);
                Thread.sleep(sleepTime);
                System.out.println("当前是第" + i + "页, 休眠时长为："+sleepTime);
                Map<String, String> queryMap = new HashMap<>();
                queryMap.put("current",i+""); //
                queryMap.put("size","100"); //
                queryMap.put("status","现行"); //
                HttpResponse httpResponse;
                httpResponse = HttpNewUtils.doPostByApplicationJson(baseUrl+"stdQueryList", null, httpClient, headers, queryMap, JSON.toJSONString(queryMap));
                String result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
                // 1569页
                Map resultMap = (Map) JSON.parse(result);
                Object records = resultMap.get("records");

//            Integer pages = (Integer) resultMap.get("pages");
                List<SacinfoModel> queryResultList = JSON.parseArray(records.toString(), SacinfoModel.class);
//            SacinfoModel(code=DB6201/T 012-2025, chName=软儿梨生产技术规程, industry=兰州市, status=现行, issueDate=1745078400000, actDate=1746028800000, recordNo=124707-2025, recordDate=1751299200000, industryClassification=null, pk=aa2e4329f4df60e8c3f265862ad918aa82755886edf9fe0f616120d9f5671484)
//            issueDate=1745078400000, actDate=1746028800000

                for (SacinfoModel sacinfoModel : queryResultList) {
                    sacinfoModel.setActDateStr(DateUtil.formatTimeStamp(sacinfoModel.getActDate()));
                    sacinfoModel.setIssueDateStr(DateUtil.formatTimeStamp(sacinfoModel.getIssueDate()));
                    sacinfoModel.setRecordDateStr(DateUtil.formatTimeStamp(sacinfoModel.getRecordDate()));
                    System.out.println(sacinfoModel.getChName() + "---" +sacinfoModel.getPk());
                    String detailPage = baseUrl + "stdDetail/" + sacinfoModel.getPk();
                    webDriver.get(detailPage);
                    wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".main-body")));
                    // 获取渲染后的页面源码
                    String pageSource = webDriver.getPageSource();

                    // 使用JSoup解析
                    Document document = Jsoup.parse(pageSource);
                    String industryClassification = document.selectFirst("dt:contains(行业分类) + dd").text().trim();
                    sacinfoModel.setIndustryClassification(industryClassification);
                    // 添加随机延迟（降低检测风险）
                    Thread.sleep(200 + new Random().nextInt(200));
                    resultList.add(sacinfoModel);
                }

                System.out.println(result);
                long currentTimeMillis = System.currentTimeMillis();
                long durationMillis = currentTimeMillis - startTime;
                long seconds = TimeUnit.MILLISECONDS.toSeconds(durationMillis) % 60;
                long minutes = TimeUnit.MILLISECONDS.toMinutes(durationMillis);
                long hours = TimeUnit.MILLISECONDS.toHours(durationMillis);
                System.out.println("总耗时：" + hours + "小时" + minutes + "分钟" + seconds + "秒");

                System.out.printf("总进度：第 %d/%d 页，进度 %.2f%%\n", i, pages, ((double) i / pages) * 100);

            }
            exportExcel(resultList);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }


    public static void exportExcel(List<SacinfoModel> resultList) {
        System.out.println("去重前总数量："+resultList.size());
        ArrayList<SacinfoModel> removeDup = resultList.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(SacinfoModel::getPk))), ArrayList::new));
        System.out.println("去重后总数量："+removeDup.size());
        String fileName = "D:\\workspace-pycharm\\spider-out\\123.xlsx";
        write(fileName, SacinfoModel.class).sheet().head(SacinfoModel.class)
                .registerWriteHandler(new CustomColumnWidthStrategy())
                .registerWriteHandler(new CustomCellStyleStrategy()).doWrite(removeDup);
        System.out.println("生成excel成功");
    }

    public static WebDriver getWebDriver(){
        Path path = Paths.get("00dowin/src/main/resources/static/chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", String.valueOf(path.toAbsolutePath()));
        ChromeOptions options = new ChromeOptions();
        // 基础设置
        options.addArguments(
                "--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
                "--window-size=1920,1080",
                "--lang=zh-CN,zh;q=0.9",
                "--disable-gpu",
                "--disable-blink-features=AutomationControlled",
                "--remote-allow-origins=*",
                "--disable-web-security",
                "--allow-running-insecure-content",
                "--disable-extensions",
                "--no-sandbox"
        );

        // 禁用自动化特征
        options.setExperimentalOption("excludeSwitches", Arrays.asList(
                "enable-automation",
                "load-extension",
                "enable-logging"
        ));
        options.setExperimentalOption("useAutomationExtension", false);
        // 创建WebDriver实例
        return new ChromeDriver(options);
    }
}
